from django.shortcuts import render

# Create your views here.
from rest_framework import status
from rest_framework.generics import ListAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView
from rest_framework.response import Response

from areas.models import Area
from areas.serializers import AreasSerializer, AddressesSerializer
from users.models import Address


class AreasView(ListAPIView):
    queryset = Area.objects.filter(parent=None)
    serializer_class = AreasSerializer


class AreaView(ListAPIView):
    serializer_class = AreasSerializer

    def get_queryset(self):
        pk = self.kwargs['pk']
        return Area.objects.filter(parent_id=pk)


class AddressesView(CreateAPIView, ListAPIView, UpdateAPIView, DestroyAPIView):

    serializer_class = AddressesSerializer

    def get_queryset(self):
        queryset = Address.objects.filter(user=self.request.user, is_deleted=False)
        return queryset

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        ser = self.get_serializer(queryset, many=True)

        return Response({'addresses': ser.data})

    def destroy(self, request, *args, **kwargs):
        instance = self.get_object()
        instance.is_deleted = True
        instance.save()

        return Response(status=status.HTTP_204_NO_CONTENT)
